﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
    <link href="../CSS/style1.css" rel="stylesheet" type="text/css" />
    <link href="../CSS/style4.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        td.rootchk { width:40px;}
        td.rootchk input { margin-left:16px; }
    </style>
    <script src="../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            Load();
            List();

            $(".rootchk input").live("click", function () {
                $(this).parent().next().find(".rootchk input").attr("checked", $0(":checked"));
            });
            $("#lab").live("click", function () {
                $("#lbl").slideToggle(); //.toggle()
            });
        })
        //得到传入过来的角色ID
        var roleID = window.dialogArguments.id;
        
        function Load() {

            //得到角色名称
            $.ajax({
                url: "rolePowerDialog.asmx/GetRoleName",
                type: "post",
                data: JSON.stringify({ roleID: roleID }),
                dataType: "json",
                contentType: "application/json",
                success: function (obj) {
                    $("#span_roleName").text(obj.d);
                }, error: function (xmlhttp) {
                    alert("错误：" + xmlhttp.responseText);
                }
            });
        }
        function List() {
            //显示出所有的菜单
            $.ajax({
                url: "rolePowerDialog.asmx/SelectMenus",
                type: "post",
                data: JSON.stringify({ roleID: roleID }),
                dataType: "json",
                contentType: "application/json",
                success: function (obj) {
                    var roots = obj.d;
                    //将查询的结果显示出来
                    var tbody = $(".tableEdit tbody");
                    tbody.empty();
                    for (var i = 0; i < roots.length; i++) {
                        var tr = $("<tr></tr>").appendTo(tbody);
                        tr.append("<td class='rootchk'><input type='checkbox' class='chk_menu" + roots[i].ID + "' value='" + roots[i].ID + "' /></td>");
                        tr.append("<td><label class='chk_menu" + roots[i].ID + "'>" + roots[i].MenuName + "</label></td>");
                        //判断该一级菜单有没有被选中
                        if (roots[i].Checked) {
                            tr.find(".rootchk input").attr("checked", "checked");
                        }
                        //显示二级菜单
                        tr = $("<tr></tr>").appendTo(tbody);
                        tr.append("<td>&nbsp;</td>");
                        tr.append("<td></td>");
                        var td = tr.find("td:eq(1)");
                        for (var j = 0; j < roots[i].SubMenus.length; j++) {
                            td.append("<label><input class='chk_menu" + roots[i].SubMenus[j].ID + "' type='checkbox' value='" + roots[i].SubMenus[j].ID + "'> " + roots[i].SubMenus[j].MenuName + "</label><br/>");
                            //判断该二级菜单有没有被勾选
                            if (roots[i].SubMenus[j].Checked) {
                                td.find(".chk_menu" + roots[i].SubMenus[j].ID).attr("checked", "checked");
                            }
                        }
                    }

                }, error: function (xmlhttp) {
                    alert("错误：" + xmlhttp.responseText);
                }
            });
        }
        //确认修改权限
        function ok() {
            
            var s = "";
            var checkeds = $(":checked");
            for (var i = 0; i < checkeds.length; i++) {
                s += checkeds.get(i).value + ",";
            }
            //请求web服务，更新权限
            $.ajax({
                url: "rolePowerDialog.asmx/UpdatePower",
                type: "post",
                data: JSON.stringify({ roleID: roleID, powers: s }),
                dataType: "json",
                contentType: "application/json",
                success: function (obj) {
                    alert("更新权限成功。");
                    window.close();
                }, error: function (xmlhttp) {
                    alert("错误：" + xmlhttp.responseText);
                }
            });
        }
    </script>
</head>
<body>
    <table class="tableEdit">
        <thead>
            <tr>
                <td colspan="2">角色权限设置 - <span id="span_roleName"></span></td>
            </tr>
        </thead>
        <tbody>
            
        </tbody>
        <tfoot>
            <tr>
                <td colspan="2">
                    <input type="button" value="确定" onclick="ok();" />&nbsp;&nbsp;
                    <input type="button" value="关闭" onclick="window.close()" />
                </td>
            </tr>
        </tfoot>
    </table>
</body>
</html>
